<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="客户名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="departName" :rules="[{required: true, message: '请输入客户名称!'}]">
              <a-input v-model="model.departName" placeholder="请输入客户名称" :readOnly="true"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="登录密码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="initPassword" has-feedback>
              <a-input type="password" v-model="model.initPassword" placeholder="请输入登录密码" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="确认密码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="confirmpassword" has-feedback>
              <a-input type="password" v-model="model.confirmpassword" placeholder="请重新输入登录密码" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { postAction, putAction } from '@/api/manage'
export default {
  name: "SysChangePasswordForm",
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data () {
    return {
      model:{
        status: 0
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        initPassword: [
          {
            required: true,
            pattern:/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9a-zA-Z]{8,16}$/,
            message: '密码由8-16位数字、大小写字母组成!'
          },
          {validator: this.validateToNextPassword}
        ],
        confirmpassword: [
          {required: true, message: '请重新输入登录密码!'},
          {validator: this.compareToFirstPassword}
        ]
      },
      url: {
        add: "/sys/sysDepart/add",
        edit: "/sys/sysDepart/edit",
        queryById: "/sys/sysDepart/queryById",
        changePassword: "/sys/sysDepart/changePassword",
      }
    }
  },
  computed: {
    formDisabled(){
      return this.disabled
    },
  },
  created () {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
  },
  methods: {
    add () {
      this.edit(this.modelDefault);
    },
    edit (record) {
      this.model = Object.assign({}, record);
      this.visible = true;
    },
    submitForm () {
      const that = this;
      // 触发表单验证
      that.$refs.form.validate(valid => {
        if (valid) {
          let httpurl = '';
          let Action;
          let params = Object.assign({}, that.model);
          if(!that.model.id){
            Action = postAction;
            httpurl+=that.url.add;
          }else{
            Action = putAction;
            httpurl+=that.url.changePassword;
            params = {
              id: that.model.id,
              initPassword: that.model.initPassword,
              confirmpassword: that.model.confirmpassword
            }
          }
          that.confirmLoading = true;
          Action(httpurl,params).then((res)=>{
            if(res.success){
              that.$message.success(res.message);
              that.$emit('ok');
            }else{
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
          })
        }
      })
    },
    validateToNextPassword  (rule, value, callback) {
      if (value && this.model.confirmpassword && value !== this.model.confirmpassword) {
        callback('两次输入的密码不一样！');
      }else{
        callback();
      }
    },
    compareToFirstPassword  (rule, value, callback) {
      if (value && this.model.initPassword && value !== this.model.initPassword) {
        callback('两次输入的密码不一样！');
      } else {
        callback()
      }
    }
  }
}
</script>

<style scoped>
.ellipsis{
  max-width: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  display: inline-block;
}
</style>